import { history } from 'umi'
import { getWorkFlowByType } from '@/pages/WorkFlow/utils';
import { message } from 'antd';

import pageTabUtil from '@/utils/pageTabUtil';
import tabMessageUtil from '@/pages/SalaryManage/utils/tabMessageUtil';

/** 关掉当前页签 */
const closeCurrentAndOpen = routeInfo => {
    pageTabUtil.closeTab(pageTabUtil.getCurrentKey());
    // 由于closeTab有用到setTimeout 0, 此处用timeOut也是迫不得已
    window.setTimeout(() => {
        history.push(routeInfo);
    }, 50);
};

/** 跳转到【流程发放】 */
const goToSalarypaysetting = () => {
    history.push({
        pathname: '/finance/salary/calculate/templatesetting/salarypaysetting',
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/** 跳转到【薪资项清单(曾用名：薪资要素)】 */
const goToPartTemplate = organizationCode => {
    const pathname = '/finance/salary/calculate/templatesetting/parttemplate';
    tabMessageUtil.memoFromTo(pathname);
    history.push({
        pathname,
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【新增薪资项（编辑薪资项）】
 * @param query
 * @param { 'add' | 'edit' | 'detail' } [operateType=add] - 操作类型
 */
const goToSalarymainadd = ({ query }, operateType = 'add') => {
    const pathname =
        {
            add: '/finance/salary/salarymainadd',
            edit: '/finance/salary/salarymainedit',
            detail: '/finance/salary/salarymaindetail'
        }[operateType] || '/finance/salary/salarymainadd';

    tabMessageUtil.memoFromTo(pathname, query);
    history.push({
        pathname,
        query,
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/** 查看审批详情 */
const goToProcessDetail = ({ processDefinitionId, processInstanceId }) => {
    history.push({
        pathname: '/personal/portal/myAuditRead/myAuditReadDetail',
        query: {
            processDefinitionId,
            processInstanceId
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/** 跳转到发放流程日志 */
const goToSalaryPaySettingLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/templatesetting/salarypaysettinglog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

const goToMedicalSocialSecurity = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/medicalsocialsecurity',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

const goToTaxCalculator = query => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/taxcalculator',
        query,
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/* 跳转到薪资模板-操作日志 */
const goToSalaryTemplateLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/templatesetting/salarytemplatelog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

// 跳转到月模板提交流程
const goToSalaryTemplateProcess = year => {
    history.push({
        pathname: '/finance/salary/process/salarytemplateprocess',
        query: {
            year
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

// 跳转到发放流程中的提交流程
const goToSalaryPaySettingProcess = () => {
    history.push({
        pathname: '/finance/salary/process/salarypaysettingprocess',
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到发起流程界面
 * @param {string} type 流程分类
 * @param {object} queryInfo query的额外信息
 * @param {string} notFoundMessage 没有流程时的提示信息
 */
const goToProcessApply = async (type, queryInfo, notFoundMessage) => {
    try {
        const flowInfo = await getWorkFlowByType(type, true);
        if (!flowInfo) {
            message.error(notFoundMessage);
            return;
        }
        const user = localStorage.getItem('user');
        const { employeeCode } = user ? JSON.parse(user) : {};
        history.push({
            pathname: '/personal/portal/apply',
            query: {
                id: flowInfo.actProcessDefinitionId,
                employeeCode,
                name: flowInfo.processName,
                ...queryInfo
            }
        });
    } catch (e) {
        message.error(e?.toString());
    }
};

const goToMedicalSocialSecurityAdd = (organizationCode, socialCityCode) => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/medicalsocialsecurityadd',
        query: {
            organizationCode,
            socialCityCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【专项扣款-操作日志】页面
 */
const goToSpecialTaxLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/importexcel/salarySpecialDeductlog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【专项扣款编辑】页面
 * @param record
 */
const goToSpecialTaxEdit = record => {
    const routeConfig = {
        pathname: '/finance/salary/calculate/importexcel/salarySpecialDeductedit',
        query: {
            id: record.id
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    };
    tabMessageUtil.memoFromTo(routeConfig.pathname, routeConfig.query);
    history.push(routeConfig);
};

/**
 * 跳转到【绩效系数编辑】页面
 * @param record
 */
const goToSalaryBaseKpiEdit = record => {
    const routeConfig = {
        pathname: '/finance/salary/calculate/importexcel/salarybasekpiedit',
        query: {
            id: record.id
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    };
    tabMessageUtil.memoFromTo(routeConfig.pathname, routeConfig.query);
    history.push(routeConfig);
};

/**
 * 跳转到个税出始化编辑页面
 * @param record
 */
const goToSalaryPayrollTaxEdit = record => {
    const routeConfig = {
        pathname: '/finance/salary/calculate/importexcel/salarypayrolltaxedit',
        query: {
            id: record.id
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    };
    tabMessageUtil.memoFromTo(routeConfig.pathname, routeConfig.query);
    history.push(routeConfig);
};

/**
 * 跳转到险金基数初始化编辑页面
 * @param record
 */
const goToPremiumBaseEdit = record => {
    const routeConfig = {
        pathname: '/finance/salary/calculate/importexcel/premiumbaseedit',
        query: {
            id: record.id
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    };
    tabMessageUtil.memoFromTo(routeConfig.pathname, routeConfig.query);
    history.push(routeConfig);
};

/**
 * 跳转到个税出始化编辑页面
 */
const goToInsuranceHousingFundLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/insurancehousingfundlog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【个人所得税-操作日志】页面
 */
const goToPersonalIncomeTaxLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/personalincometaxlog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};
/**
 * 跳转到【绩效系数-操作日志】页面
 */
const goToSalaryBaseKpiLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/importexcel/salarybasekpilog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};
/**
 * 跳转到【个税出始化-操作日志】页面
 */
const goToSalaryPayrollTaxLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/importexcel/salarypayrolltaxlog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【险金基数初始化-操作日志】页面
 */
const goToPremiumBaseLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/importexcel/premiumbaselog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【险金档案-待生效界面】页面
 */
const goToMedicalSocialSecurityInProcess = (organizationCode, socialCityCode) => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/medicalsocialsecurityinprocess',
        query: {
            effect: 1,
            socialCityCode,
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【个税出始化-待生效界面】页面
 */
const goToPersonalIncomeTaxInProcess = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/personalincometaxinprocess',
        query: {
            effect: 1,
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【薪资复核-操作日志】页面
 */
const goToSalaryRecheckLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/recheck/recheckLog',
        state: {
            tabRoute: true,
            tabRefresh: true
        },
        query: {
            organizationCode
        }
    });
};

/**
 * 跳转到【hr复核-详情】页面
 */
const goToSalaryHrRecheckDetail = () => {
    history.push({
        pathname: '/finance/salary/calculate/recheck/hrrecheck/detail',
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【财务复核-详情】页面
 */
const goToSalaryFinancialRecheckDetail = () => {
    history.push({
        pathname: '/finance/salary/calculate/recheck/financialrecheck/detail',
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【薪资复核-数据详情】页面
 */
const goToSalaryRecheckDataChart = data => {
    history.push({
        pathname: '/finance/salary/calculate/recheck/dataChart',
        state: {
            tabRoute: true,
            tabRefresh: true
        },
        query: {
            ...data
        }
    });
};

/**
 * 跳转到【薪资复核-操作日志】页面
 */
const goToSalaryFeedbackLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/confirm/salaryfeedbacklog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【薪资复核-操作日志】页面
 */
const goToSalaryStatisticsDetail = ({ employeeCode, organizationCode }) => {
    history.push({
        pathname: '/finance/salary/statistics/salary/salarystatisticsdetail',
        query: {
            employeeCode,
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【公积金档案（CPF SETTING）】页面
 */
const gotoCPFSetting = ({ organizationCode }) => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/cpfsetting',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【公积金档案（CPF SETTING）】页面
 */
const gotoCPFSettingInProgress = (organizationCode, cpfId) => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/cpfsettinginprogress',
        query: {
            effect: 1,
            organizationCode,
            cpfId
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【技能发展税-待生效界面】页面
 */
const goToSkillsDevelopmentTaxInProcess = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/skillsDevelopmentTaxInProcess',
        query: {
            effect: 1,
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/* 跳转到个税出始化编辑页面
 */
const goToCpfSettingLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/cpfsettinglog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【种族自助金-待生效界面】页面
 */
const goToSelfHelpFundsInProcess = (organizationCode, shgId) => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/selfhelpfundsinprocess',
        query: {
            effect: 1,
            organizationCode,
            shgId
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 跳转到【种族自助金-操作日志】页面
 */
const goToSelfHelpFundsLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/selfhelpfundslog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/** 查看技能发展税审批详情 */
const goToSkillsDevelopmentTaxProcessDetail = ({ processDefinitionId, processInstanceId }) => {
    history.push({
        pathname: '/personal/portal/myAuditRead/myAuditReadDetail',
        query: {
            processDefinitionId,
            processInstanceId
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

/**
 * 技能发展税 跳转到发起流程界面
 * @param {string} type 流程分类
 * @param {object} queryInfo query的额外信息
 * @param {string} notFoundMessage 没有流程时的提示信息
 */
const goToSkillsDevelopmentTaxProcessApply = async (type, queryInfo, notFoundMessage) => {
    try {
        const flowInfo = await getWorkFlowByType(type, true);
        if (!flowInfo) {
            message.error(notFoundMessage);
            return;
        }
        const user = localStorage.getItem('user');
        const { employeeCode } = user ? JSON.parse(user) : {};
        history.push({
            pathname: '/personal/portal/apply',
            query: {
                id: flowInfo.actProcessDefinitionId,
                employeeCode,
                name: flowInfo.processName,
                ...queryInfo
            }
        });
    } catch (e) {
        message.error(e?.toString());
    }
};
/**
 * 跳转到【技能发展税-操作日志】页面
 */
const goToSkillsDevelopmentTaxLog = organizationCode => {
    history.push({
        pathname: '/finance/salary/calculate/insurancefundfile/skillsDevelopmentTaxLog',
        query: {
            organizationCode
        },
        state: {
            tabRoute: true,
            tabRefresh: true
        }
    });
};

export {
    closeCurrentAndOpen,
    goToSalarypaysetting,
    goToPartTemplate,
    goToSalarymainadd,
    goToProcessDetail,
    goToSalaryPaySettingLog,
    goToMedicalSocialSecurity,
    goToTaxCalculator,
    goToSalaryTemplateLog,
    goToSalaryTemplateProcess,
    goToSalaryPaySettingProcess,
    goToProcessApply,
    goToMedicalSocialSecurityAdd,
    goToSpecialTaxLog,
    goToSpecialTaxEdit,
    goToSalaryBaseKpiEdit,
    goToSalaryPayrollTaxEdit,
    goToPremiumBaseEdit,
    goToInsuranceHousingFundLog,
    goToPersonalIncomeTaxLog,
    goToSalaryBaseKpiLog,
    goToSalaryPayrollTaxLog,
    goToPremiumBaseLog,
    goToMedicalSocialSecurityInProcess,
    goToPersonalIncomeTaxInProcess,
    goToSalaryRecheckLog,
    goToSalaryHrRecheckDetail,
    goToSalaryFinancialRecheckDetail,
    goToSalaryRecheckDataChart,
    goToSalaryFeedbackLog,
    goToSalaryStatisticsDetail,
    gotoCPFSetting,
    gotoCPFSettingInProgress,
    goToCpfSettingLog,
    goToSkillsDevelopmentTaxInProcess,
    goToSkillsDevelopmentTaxProcessDetail,
    goToSkillsDevelopmentTaxProcessApply,
    goToSkillsDevelopmentTaxLog,
    goToSelfHelpFundsInProcess,
    goToSelfHelpFundsLog
};
